/*
 * @lc app=leetcode id=108 lang=cpp
 *
 * [108] Convert Sorted Array to Binary Search Tree
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        int len = nums.size();
        if (len < 1) {
            return NULL;
        }
        return sortedArrayToBST(nums, 0, len-1);
    }
    TreeNode* sortedArrayToBST(vector<int>& nums, int st, int ed) {
        if (st > ed) {
            return NULL;
        } else if (st == ed) {
            return new TreeNode(nums[st]);
        }

        int mid = (st+ed)/2;
        TreeNode *root = new TreeNode(nums[mid]);
        root->left = sortedArrayToBST(nums, st, mid-1);
        root->right = sortedArrayToBST(nums, mid+1, ed);
        return root;
    }
};

